<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('钱包信息')"/>
    <th:block th:include="include :: layout-latest-css"/>
    <th:block th:include="include :: ztree-css"/>
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row">
        <div class="panel panel-default">
            <div class="panel-heading">
                <button th:text="获取挖矿钱包账号" class="btn btn-success" th:onclick="get_miner_address()"/>
                <span/>
            </div>
            <table class="table">
                <thead id="accountlist">
                <tr>
                    <th>
                        地址
                    </th>
                    <th>
                        私钥
                    </th>
                    <th>
                        余额
                    </th>
                </tr>
                </thead>
                <tbody>

                </tbody>
            </table>
        </div>
        <br/>
        <div class="panel panel-default">
            <div class="panel-heading">
                <button th:text="获取当前节点所有钱包账户" class="btn btn-primary" th:onclick="list()"/>
                <span/>
                <button th:text="创建一个新的钱包账户" class="btn btn-danger" th:onclick="new_account()"/>
            </div>
            <table class="table">
                <thead id="walletlist">
                <tr>
                    <th>
                        地址
                    </th>
                    <th>
                        私钥
                    </th>
                    <th>
                        余额
                    </th>
                </tr>
                </thead>
                <tbody>

                </tbody>
            </table>
        </div>
        <br/>
    </div>

</div>
<th:block th:include="include :: footer"/>
<script th:inline="javascript">

    window.onload = function () {
        get_miner_address();
        list();
    }


    function get_miner_address() {
        $.ajax({
            type: "GET",
            url: "http://localhost/api/account/get_miner_address",
            dataType: "json",
            success: function (data) {
                if (data.message == "SUCCESS") {
                    $("#accountlist").nextAll().remove();
                    $("#accountlist").after("<tr>\n" +
                        "                    <th>\n" +
                        "                        " + data.item.address + "\n" +
                        "                    </th>\n" +
                        "                    <th>\n" +
                        "                        " + data.item.priKey + "\n" +
                        "                    </th>\n" +
                        "                    <th>\n" +
                        "                        " + data.item.balance + "\n" +
                        "                    </th>\n" +
                        "                </tr>");
                }
            }
        });
    }

    function list() {
        $.ajax({
            type: "GET",
            url: "http://localhost/api/account/list",
            dataType: "json",
            success: function (data) {
                if (data.message == "SUCCESS") {
                    $("#walletlist").nextAll().remove();
                    for (var x = 0; x < data.item.length; x++) {
                        console.log(data.item[x]);
                        $("#walletlist").after("<tr>\n" +
                            "                    <th>\n" +
                            "                        " + data.item[x].address + "\n" +
                            "                    </th>\n" +
                            "                    <th>\n" +
                            "                        " + data.item[x].priKey + "\n" +
                            "                    </th>\n" +
                            "                    <th>\n" +
                            "                        " + data.item[x].balance + "\n" +
                            "                    </th>\n" +
                            "                </tr>");
                    }
                }
            }
        });
    }

    function new_account() {
        $.ajax({
            type: "GET",
            url: "http://localhost/api/account/new_account",
            dataType: "json",
            success: function (data) {
                if (data.message == "New account created, please remember your Address and Private Key.") {
                    list();
                }
            }
        });
    }


</script>
</body>
</html>